/*
 * Global.cpp
 *
 *  Created on: 2013-1-23
 *      Author: heqiu
 */

#include "Global.h"

GlobalData::GlobalData() {
	m_pGuudScreen = NULL;
	m_pGuudCanvasMap = NULL;
	m_pGuudCanvasWelcome = NULL;
	m_pGuudCanvasControl = NULL;
	m_pConfig_map = NULL;
}
GlobalData::~GlobalData() {
	if (m_pGuudScreen != NULL) {
		delete m_pGuudScreen;
		m_pGuudScreen = NULL;
	}
	if (m_pGuudCanvasMap != NULL) {
		delete m_pGuudCanvasMap;
		m_pGuudCanvasMap = NULL;
	}
	if (m_pGuudCanvasWelcome != NULL) {
		delete m_pGuudCanvasWelcome;
		m_pGuudCanvasWelcome = NULL;
	}
	if (m_pGuudCanvasControl != NULL) {
		delete m_pGuudCanvasControl;
		m_pGuudCanvasControl = NULL;
	}
	if (m_pConfig_map != NULL) {
		delete m_pConfig_map;
		m_pConfig_map = NULL;
	}
}
GlobalData g_GlobalData;

void pressed_button_exit(void * pData, GuudButton * pButton) {
	config_save_update();
	bool bFlag = g_GlobalData.m_config_save.save();
	if (!bFlag) {
		g_GlobalData.m_GuudLog.log("[%s][%d]错误:游戏保存", __FILE__, __LINE__);
	}
	g_GlobalData.m_GuudLog.log("[%s][%d]点击退出", __FILE__, __LINE__);
	g_GlobalData.m_GameListener.m_bQuit = true;
}

void pressed_button_begin(void * pData, GuudButton * pButton) {
	g_GlobalData.m_pGuudCanvasWelcome->m_bListen = false;
	g_GlobalData.m_pGuudCanvasWelcome->m_bShow = false;
	((CanvasWelcome*) g_GlobalData.m_pGuudCanvasWelcome)->m_button_begin.m_bListen =
			false;
	((CanvasWelcome*) g_GlobalData.m_pGuudCanvasWelcome)->m_button_begin.m_bShow =
			false;

	bool bFlag = g_GlobalData.m_config_save.loadFile("./conf/save.xml");
	if (!bFlag) {
		g_GlobalData.m_GuudLog.log("[%s][%d]错误:加载用户配置失败", __FILE__, __LINE__);
		exit(-1);
	}
	int32_t iStory = g_GlobalData.m_config_save.getKKValueInt32("schedule",
			"story");
	string sStoryKey = "./conf/story_" + GuudComm::getIntStr(iStory) + ".xml";
	bFlag = g_GlobalData.m_config_story.loadFile(sStoryKey.c_str());
	if (!bFlag) {
		g_GlobalData.m_GuudLog.log("[%s][%d]错误:加载故事配置失败", __FILE__, __LINE__);
		exit(-1);
	}
	{
		//初始化控制面板
		CanvasControl * pGuudCanvas = new CanvasControl(0, 0, 0);
		pGuudCanvas->init();
		g_GlobalData.m_pGuudCanvasControl = pGuudCanvas;
	}
	{
		//初始化游戏界面
		uint32_t uiShortUnit =
				(uint32_t) g_GlobalData.m_config_story.getKKValueInt32("canvas",
						"short-unit");
		uint32_t uiLongUnit =
				(uint32_t) g_GlobalData.m_config_story.getKKValueInt32("canvas",
						"long-unit");
		uint32_t uiUnitPixel =
				(uint32_t) g_GlobalData.m_config_story.getKKValueInt32("canvas",
						"unit-pixel");
		CanvasStory * pGuudCanvas = new CanvasStory(uiShortUnit, uiLongUnit,
				uiUnitPixel);
		pGuudCanvas->m_iStoryNo = iStory;
		pGuudCanvas->init();
		g_GlobalData.m_pGuudCanvasMap = pGuudCanvas;
	}
}
void pressed_button_save(void * pData, GuudButton * pButton) {
	config_save_update();
	bool bFlag = g_GlobalData.m_config_save.save();
	if (!bFlag) {
		g_GlobalData.m_GuudLog.log("[%s][%d]错误:游戏保存", __FILE__, __LINE__);
	}
}
void config_save_update() {
	if (g_GlobalData.m_pGuudCanvasMap != NULL) {
		int32_t iStory =
				((CanvasStory *) g_GlobalData.m_pGuudCanvasMap)->m_iStoryNo;
		g_GlobalData.m_config_save.setKKValueStr("schedule", "story",
				GuudComm::getIntStr(iStory).c_str());
		string sKey = "story-" + GuudComm::getIntStr(iStory);
		string sX = GuudComm::getIntStr(
				g_GlobalData.m_pGuudCanvasMap->m_gpScreenPosPixel.m_iX);
		string sY = GuudComm::getIntStr(
				g_GlobalData.m_pGuudCanvasMap->m_gpScreenPosPixel.m_iY);
		g_GlobalData.m_config_save.setKKValueStr(sKey.c_str(), "x-pixel",
				sX.c_str());
		g_GlobalData.m_config_save.setKKValueStr(sKey.c_str(), "y-pixel",
				sY.c_str());
	}
}
